package com.alphacsp.theedge2012.ui.activities;

import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import com.alphacsp.theedge2012.R;
import com.alphacsp.theedge2012.data.database.DataAccessor;

/**
 * Created by IntelliJ IDEA.
 * User: Gilad
 * Date: 22/10/10
 * Time: 14:38
 */
public class SpeakerDetailsActivity extends TabActivity {

    private ImageView speakerImage;
    private DataAccessor dataAccessor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.speaker_details_activity);
        dataAccessor = DataAccessor.getSingleton(this);
        final String speakerName = getIntent().getStringExtra("speakerName");
        final String speakerCompany = getIntent().getStringExtra("speakerCompany");
        final String speakerBio = getIntent().getStringExtra("speakerBio");
        final String speakerImageUrl = getIntent().getStringExtra("speakerImageUrl");

        final TabHost host = getTabHost();
        host.addTab(host.newTabSpec("Bio").setIndicator(buildIndicator("Bio")).setContent(R.id.speaker_bio_tab));
//        host.addTab(host.newTabSpec("Presentation").setIndicator(buildIndicator("Presentation")).setContent(R.id.speaker_bio_tab));

        final TextView speakerNameTextView = (TextView) findViewById(R.id.speaker_bio_tab_name);
        final TextView speakerCompanyTextView = (TextView) findViewById(R.id.speaker_bio_tab_company);
        final TextView speakerBioTextView = (TextView) findViewById(R.id.speaker_bio_tab_bio);
        speakerImage = (ImageView) findViewById(R.id.speaker_bio_tab_image);
        speakerNameTextView.setText(speakerName);
        speakerCompanyTextView.setText(speakerCompany);
        speakerBioTextView.setText(speakerBio);

        Intent intent = new Intent(SpeakerDetailsActivity.this, SessionsActivity.class);
        intent.putExtra("speakerName", speakerName);
        host.addTab(host.newTabSpec("Sessions").setIndicator(buildIndicator("Sessions")).setContent(intent));


        new SpeakerImageFetcher().execute(speakerImageUrl);
    }

    @Override
    protected void onPause() {
        super.onPause();
        dataAccessor.closeDatabase();
    }

    private void setSpeakerImage(Bitmap image) {
        speakerImage.setVisibility(View.VISIBLE);
        speakerImage.setImageBitmap(image);
    }

    private View buildIndicator(String text) {
        final TextView indicator = (TextView) getLayoutInflater().inflate(R.layout.tab_indicator, getTabWidget(), false);
        indicator.setText(text);
        return indicator;
    }

    @SuppressWarnings({"UnusedDeclaration"})
    public void onHomeClick(View view) {
        final Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

    @SuppressWarnings({"UnusedDeclaration"})
    public void onSearchClick(View v) {
        onSearchRequested();
    }

    private class SpeakerImageFetcher extends AsyncTask<String, Void, Bitmap> {
        @Override
        protected Bitmap doInBackground(String... strings) {
            final String url = strings[0];
            return dataAccessor.fetchImage(url);
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            setSpeakerImage(bitmap);
        }

    }


}
